package de.jamm.gui;

import java.awt.Point;
import java.awt.Polygon;
import java.util.HashSet;


public class Stone{
	private static HashSet<Stone> allStones = new HashSet<Stone>();
	private int type = 0;
	private int x = 0;
	private int y = 0;
	private int moveX = 0;
	private int moveY = 0;
	private long moveMillis = 0;
	private Polygon flaeche = null;
	private boolean move = false; 
	private int startX = 0;
	private int startY = 0;
	private boolean delete = false;
	private MapField mapfield = null;
	private int id;
	
	private boolean selected = false;
	
	public Stone(int type, int x, int y, int id) {
		// TODO Auto-generated constructor stub
		this.type = type;
		this.x = x;
		this.y = y;
		this.startX = x;
		this.startY = y;
		this.id = id;
		
		refreshFlaeche();
		
		allStones.add(this);
	}
	protected static Stone getStoneById(int id){
		for(Stone temp:allStones){
			if(temp.getId() == id){
				return temp;
			}
		}
		return null;
	}
	protected static Stone getUnuseStone(int type){
		for(Stone temp:allStones){
			System.out.println(temp.isStartPos());
			System.out.println(temp.getType()+" == "+type);
			if(temp.isStartPos() && temp.getType() == type){
				return temp;
			}
		}
		return null;
	}
	protected static HashSet<Stone> getAllStones() {
		return allStones;
	}
	protected static void setAllStones(HashSet<Stone> allStones) {
		Stone.allStones = allStones;
	}
	protected int getType() {
		return type;
	}
	protected void setType(int type) {
		this.type = type;
	}
	protected int getX() {
		return x;
	}
	protected void setX(int x) {
		this.x = x;
	}
	protected int getY() {
		return y;
	}
	protected void setY(int y) {
		this.y = y;
	}
	protected boolean isSelected() {
		return selected;
	}
	protected void setSelected(boolean selected) {
		if(GuiController.getInstance().isWaitForRemove()){
			if(this.getMapfield() != null){
				GuiController.getInstance().sendRemove(this.getMapfield().getFieldCount(),this.type);
			}
		}else{
			if(selected){
				Stone selectedStone = getSelected();
				if(selectedStone != null)
					selectedStone.setSelected(false);
			}
			
			this.selected = selected;
		}
	}
	protected void deleteStone() {
		// TODO Auto-generated method stub
		this.setDelete(true);
		this.setX(0);
		this.setY(0);
		refreshFlaeche();
	}
	protected static Stone getSelected(){
		for(Stone temp:allStones){
			if(temp.isSelected())
				return temp;
		}
		
		return null;
	}
	protected boolean contains(Point p){
		refreshFlaeche();
		return flaeche.contains(p);
	}
	protected void refreshFlaeche() {
		// TODO Auto-generated method stub
		flaeche = new Polygon();
		flaeche.addPoint(x, y);
		flaeche.addPoint(x+60, y);
		flaeche.addPoint(x+60, y+60);
		flaeche.addPoint(x, y+60);
	}
	protected static Stone getStoneByPoint(Point point) {
		// TODO Auto-generated method stub
		for(Stone temp:getAllStones()){
			if(temp.contains(point)){
				return temp;
			}
		}
		return null;
	}
	protected Polygon getFlaeche() {
		return flaeche;
	}
	protected void setFlaeche(Polygon flaeche) {
		this.flaeche = flaeche;
	}
	protected void sendMove(MapField mf,long millis) {
		// TODO Auto-generated method stub
		GuiController.getInstance().sendMove(this.getMapfield(),mf,getType(),this);
	}
	protected void moveTo(MapField mf,final long millis) {
		// TODO Auto-generated method stub
		moveX = mf.getX();
		moveY = mf.getY();
		mapfield = mf;
		moveMillis = millis;
		mf.setCurrStone(this);
		
	   Thread mover = new Thread(new Runnable() {
			private boolean work = true;
			private boolean typeX = false;
			private boolean typeY = false;
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				work = true;
				setMove(true);
				
				if(getMoveX() >= getX())
					typeX = true;
				else if(getMoveX() < getX())
					typeX = false;
				
				if(getMoveY() >= getY())
					typeY = true;
				else if(getMoveY() < getY())
					typeY = false;
					
				while(work){
					if(typeX){
						moveX();
						if(getMoveX() <= getX()){
							work = false;
							setX(getMoveX());
						}
					}else{
						moveX();
						if(getMoveX() >= getX()){
							work = false;
							setX(getMoveX());
						}
					}
					
					
					
					GuiController.getInstance().repaintGui();
					
					try {
						Thread.currentThread();
						Thread.sleep(getMoveMillis());
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				try {
					Thread.currentThread();
					Thread.sleep(300);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				work = true;
				while(work){
					if(typeY){
						moveY();
						if(getMoveY() <= getY()){
							work = false;
							setY(getMoveY());
						}
					}else{
						moveY();
						if(getMoveY() >= getY()){
							work = false;
							setY(getMoveY());
						}
					}
					
					GuiController.getInstance().repaintGui();
					
					try {
						Thread.currentThread();
						Thread.sleep(getMoveMillis());
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				setMove(false);
				GuiController.getInstance().repaintGui();
			}
			private void moveX() {
				// TODO Auto-generated method stub
				int xOld = getX();
				if(!typeX)
					setX(xOld-10);
				else
					setX(xOld+10);
			}
			private void moveY() {
				// TODO Auto-generated method stub
				int yOld = getY();
				if(!typeY)
					setY(yOld-10);
				else
					setY(yOld+10);
			}
		});
		mover.start();
	}
	protected int getMoveX() {
		return moveX;
	}
	protected void deleteToken(){
		//this.moveTo(startX, startY, 50);
	}
	protected boolean isStartPos(){
		if(x == startX && y == startY)
			return true;
		return false;
	}
	protected void setMoveX(int moveX) {
		this.moveX = moveX;
	}
	protected int getMoveY() {
		return moveY;
	}
	protected void setMoveY(int moveY) {
		this.moveY = moveY;
	}
	protected long getMoveMillis() {
		return moveMillis;
	}
	protected void setMoveMillis(long moveMillis) {
		this.moveMillis = moveMillis;
	}
	protected boolean isMove() {
		return move;
	}
	protected MapField getMapfield() {
		return mapfield;
	}
	protected void setMapfield(MapField mapfield) {
		this.mapfield = mapfield;
	}
	protected void setMove(boolean move) {
		this.move = move;
	}
	protected boolean isDelete() {
		return delete;
	}
	protected void setDelete(boolean delete) {
		this.delete = delete;
	}
	protected int getId() {
		return id;
	}
	protected void setId(int id) {
		this.id = id;
	}
	protected static Stone getStoneByMapField(MapField mf) {
		// TODO Auto-generated method stub
		return mf.getCurrStone();
//		for(Stone temp:allStones){
//			if(temp.getMapfield() != null && temp.getMapfield().equals(fieldByFieldCount)){
//				return temp;
//			}
//		}
		
		//return null;
	}
	public static void resetAllStones(){
		allStones.clear();
		
		int y = 60;
		int y_2 = 80;
		for (int i = 0; i <= 8; i++) {
			new Stone(1, -80, i * y,i);
		}
		for (int i = 0; i <= 8; i++) {
			new Stone(2, 630, i * y + y_2,i+9);
		}
	}
}
